class CustomersController < ApplicationController
  before_action :set_menu

  def index
    @submenu = :customer_list
    @title = '客户档案'
    @tasks_grid = initialize_grid(Customer, per_page: 8, order: 'id', order_direction: 'desc')
  end

  def new
    @title = "客户档案/新增"
    @buttons.push :action => customers_path, :label => "返回"
  	@customer = Customer.new
    if params.has_key? :car_mo_check
      @car_brand = CarBrand.find params[:customer][:car_brand_id]
      @car_model = CarModel.find params[:customer][:car_model_id]
    end
  end

  def create
    @customer = Customer.new post_params
    if @customer.save
      redirect_to customers_path
    else
      render :action => 'new'
    end
  end

  def edit
    @title = '客户档案/修改'
    @insurers = Insurer.all
    @customer = Customer.find params[:id]
    @buttons.push :action => customers_path, :label => "返回"
    @customer_levels = CustomerLevel.all
    if params.has_key? :car_mo_check
      @car_brand = CarBrand.find params[:customer][:car_brand_id]
      @car_model = CarModel.find params[:customer][:car_model_id]
    end
  end

  def update
    @customer = Customer.find params[:id]
    if @customer.update_attributes post_params
      redirect_to customers_path
    else
      render :action => 'edit'
    end
  end

  def show
    @title = "客户档案/明细"
    @buttons.push :action => customers_path, :label => "返回"
    @customer = Customer.find params[:id]
    if params[:bdetail] && params[:show]='idle1'
      @order_beauty = OrderBeauty.find params[:bdetail]
    elsif params[:mdetail] && params[:show]='idle2'
      @order_maintain = OrderMaintain.find params[:mdetail]
    end

  end

  def destroy
    @customer = Customer.find params[:id]
    @customer.destroy
    redirect_to customers_path
  end

  def customer_check
    @area = [ "粤", "京", "津", "沪", "渝", "冀", "豫", "云", "辽", "黑", "湘", "皖", "鲁", "新", "苏", "浙", "赣", "桂", "甘", "晋", "蒙", "陕", "吉", "闽", "贵", "青", "藏", "琼"]
    @letter = [ "S", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "T", "U", "V", "W", "X", "Y", "Z"]
  end


  def post_params
    params.require(:customer).permit(*%w(customer_level_id name phone car_num car_mo car_colour car_fnum car_enum account telephone age id_num customer_source qq birthday addr customer_type email occupation zip_code sex driving_expire fax of_date buy_date force_expire commercial_expire last_cf_date last_cf_mileage last_care_date last_care_mileage fix_care_mileage fix_care_cycle next_care_date next_care_mileage insurer_id year_inspect_date active_status oil_meter))
  end

  private
  def set_menu
    @mainmenu = :customer
  end
end
